package com.wanglei.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.wanglei.ssm.domain.Product;
import com.wanglei.ssm.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 商品控制器
 *
 * @author wl
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    /**
     * 商品服务
     */
    @Autowired
    private ProductService productService;

    /**
     * 商品信息分页查询接口
     *
     * @param page     当前页码
     * @param pageSize 每页展示条数
     * @return 商品信息集合
     * @throws Exception e
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findAllByPage(page, pageSize);
        mv.addObject("productList", products);
        mv.setViewName("product-list");
        return mv;
    }

    /**
     * 商品保存接口
     *
     * @param product 需要保存的商品信息
     * @throws Exception e
     */
    @RequestMapping("/save.do")
    public String save(Product product) throws Exception {
        productService.save(product);
        return "redirect:findAll.do";
    }
}
